<template>
  <j-modal
    centered
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    @cancel="close"
    cancelText="关闭">
    <j-form-container>
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="选择方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-single-select-tag
                type="list_multi"
                v-model="model.type"
                :options="typeOptions"
                placeholder="选择方式"
                :disabled="attendanceModalTitle() === '编辑'"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.type === '2'" style="height: 331px" prop="sex">
            <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sex">
              <j-dict-select-tag v-model="model.sex" type="radioButton" title="性别" dictCode="sex" placeholder="请选择性别"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <j-select-user-and-dep ref="userDepRef" v-show="model.type === '1'" />
  </j-modal>
</template>

<script>
  import { getAction } from '@/api/manage'
  import { cloneObject, pushIfNotExist } from '@/utils/util'
  import JSelectUserAndDep from './JSelectUserAndDep';
  import { getSexVal } from './userDepSex.js'

  export default {
    name: 'JSelectUserDepSexComponentModal',
    components: {JSelectUserAndDep},
    props: {
      value: {
        type: Array,
        default: () => []
      },
      visible: {
        type: Boolean,
        default: false
      },
      width: {
        type: Number,
        default: 900
      },
      //弹窗标题
      title: {
        type: String,
        default: '考勤人员选择'
      },
      selectOptions: {
        type: Array,
        default: () => []
      },
    },
    data() {
      return {
        model:{
          type: '1'
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 4 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        validatorRules: {
          type: [
            { required: true, message: '选择方式不能为空!'},
          ],
          sex: [
            { required: true, message: '请选择性别!'},
          ],
        },
        typeOptions: [
          {
            label: '按部门与人员选择',
            value: '1'
          },
          {
            label: '按性别选择',
            value: '2'
          }
        ],
      }
    },
    inject: {
      attendanceModalTitle: "attendanceModalTitle",
    },
    watch: {
      visible: {
        immediate: true,
        handler(val) {
          //避免selectOptions数组长度不变，无法监听，用visible监听
          if(val){
            this.$nextTick(() => {
              if(this.selectOptions?.[0]?.type === '2'){
                let value = this.selectOptions?.[0]?.value || '';
                //解决赋值失败的问题
                this.$set(this.model, 'sex', value);
                this.$set(this.model, 'type', '2');
                return;
              }
              //将数据传入组件
              this.$set(this.model, 'type', '1');
              this.$refs.userDepRef.selectOptions = this.selectOptions;
            })
          }
        }
      }
    },

    methods: {
      /** 关闭弹窗 */
      close() {
        this.$emit('update:visible', false)
      },

      /** 完成选择 */
      handleOk() {
        this.$refs.form.validate(valid => {
          if(valid){
            if(this.model.type === '1'){
              //部门人员
              let value = this.$refs.userDepRef.userDepList?.map(item => {
                return item.id
              })
              let options = this.$refs.userDepRef.userDepList?.map(item => {
                if(item.children || item.parentId){
                  //部门
                  return {
                    ...item,
                    label: item.title,
                    value: item.id,
                    orgId: item.orgCode,
                    type: '0'
                  }
                }else{
                  //人员
                  return {
                    ...item,
                    label: item.username,
                    value: item.id,
                    orgId: item.orgCode,
                    type: '1'
                  }
                }
              })
              //修改数据展示
              this.$emit('input', value);
              //修改下拉数据源
              this.$emit('options', options);
            }else if(this.model.type === '2'){
              //性别
              //修改数据展示
              this.$emit('input', [this.model.sex]);
              //修改下拉数据源
              this.$emit('options', [{
                label: getSexVal(this.model.sex),
                value: this.model.sex,
                type: '2'
              }])
            }
            this.close()
          }
        })
      },
    }
  }
</script>
<style lang="less" scoped>
</style>